#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 1. 读取目标图片 并设置到按钮上
    QIcon icon("://images/helicopter.png");
    ui->pushButton_icon->setIcon(icon);
    ui->pushButton_icon->setIconSize(QSize(100, 100));

    // 1.5 设置四个方向键
    ui->pushButton_up->setIcon(QIcon(":/images/up1.png"));
    ui->pushButton_down->setIcon(QIcon(":/images/down1.png"));
    ui->pushButton_left->setIcon(QIcon(":/images/left1.png"));
    ui->pushButton_right->setIcon(QIcon(":/images/right1.png"));

    // 3. 设置按钮快捷键
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_Up));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_Down));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_Left));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_Right));

    // 3.5 开启鼠标连发功能
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}


// 2. 实现四个方向按钮的槽函数
void Widget::on_pushButton_up_clicked()
{
    // 将图片上移
    QRect rect = ui->pushButton_icon->geometry();
    ui->pushButton_icon->setGeometry(rect.x(), rect.y() - 10, rect.width(), rect.height());
}


void Widget::on_pushButton_down_clicked()
{
    // 将图片下移
    QRect rect = ui->pushButton_icon->geometry();
    ui->pushButton_icon->setGeometry(rect.x(), rect.y() + 10, rect.width(), rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    // 将图片左移
    QRect rect = ui->pushButton_icon->geometry();
    ui->pushButton_icon->setGeometry(rect.x() - 10, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    // 将图片右移
    QRect rect = ui->pushButton_icon->geometry();
    ui->pushButton_icon->setGeometry(rect.x() + 10, rect.y(), rect.width(), rect.height());
}

Widget::~Widget()
{
    delete ui;
}
